import {isMobile} from "../../../utils/util";
import ExamAPI from "../../../repo/api/ExamAPI";

let ExamResultMixin = {
  data() {
    return {
      result: {},
      totalRightCnt: 0,

    }
  },
  computed: {
    isMobile: () => isMobile(),
    params() {
      return this.$route.params
    },
    subjectId() {
      return this.params.subjectId
    },
    examId() {
      return this.params.examId
    },
    examResultId() {
      return this.params.examResultId
    },
    type(){
      if(this.params.type){
        return this.params.type
      }else{
        return ''
      }
    },
    errorType() {
      return this.params.errorType
    },
    totalScore() {
      return this.result.totalScore
    },
    passScore() {
      return this.result.passScore
    },
    isPassed() {
      return this.result.score >= this.passScore
    },
    isMockExam() {
      return this.result.examType === 3
    },
    canRepeat() {
      return this.result.repeatType === 1
    }

  },
  created() {
    console.log("exam-result-mixin", 'this.params=', this.params)
    this.fetchData()
  },
  methods: {
    fetchData() {
      let examResultId = this.examResultId;
      let errorType = this.errorType;
      ExamAPI.getExamResult({examResultId,errorType})
        .then(result => {
          this.result = result.responseData
        })

      ExamAPI.getExamAnalyzeById(this.examResultId).then((result) => {
        let data = result.responseData
        let logs = data.logs;
        let questions = data.questions
        //非综合题答对的数量
        let normalRightCnt = logs.filter(log => log["examQuestionMainId"] === 0).filter(log => log.score > 0).length;
        //综合题答对的数量
        let mixRightCnt = this.calcMixRightCnt(questions, logs);
        this.totalRightCnt = normalRightCnt + mixRightCnt;
      })
    },
    calcMixRightCnt: function (questions, logs) {
      //综合题
      let mixList = questions.filter(q => q["questions"])
      //记录每道综合题的总分
      let mixQuestionScoreMap = {}
      mixList.forEach(mix => {
        mix['questions'].forEach(q => {
          mixQuestionScoreMap[mix.id] = mixQuestionScoreMap[mix.id] || 0
          mixQuestionScoreMap[mix.id] += q['score']
        })
      })

      //记录每到综合题答案的得分
      let mixLogsScoreMap = {}
      logs.filter(log => log['examQuestionMainId'] > 0)
        .forEach(log => {
          let mixId = log['examQuestionMainId'];
          mixLogsScoreMap[mixId] = mixLogsScoreMap[mixId] || 0
          mixLogsScoreMap[mixId] += log['score']
        })
      let mixRightCnt = 0
      for (let key in mixQuestionScoreMap) {
        if (mixLogsScoreMap[key] > 0) mixRightCnt++
      }
      return mixRightCnt;
    },
  }
}
export default ExamResultMixin
